<?php
class classPriceExport{
    private $iUserID;

    function __construct($UserID=0){
        $_US = new classUsersService();
        $this->iUserID = $_US->iGetIdOfLoggedUser();
        //if user havent got discount his ID is set to 0 (zero)
        if(!$_US->getUserDiscountPermission($this->iUserID)) $this->iUserID = 0;
    }
        
    //only this function has to be updated for user discount !!!!!!!!!!!!!!
    public function getDBPriceVat($iID,&$fPrice,&$fTax){
        $sql_result	= mysql_query('select Product_Price, Product_DPH from products where ID='.$iID);
        $sql_object	= mysql_fetch_object($sql_result);
        $fPrice 	= $sql_object->Product_Price;
        $fTax       = $sql_object->Product_DPH;
        //discount:
        if(
                $this->iUserID != '' and
                is_numeric($this->iUserID) and
                $this->iUserID>0
        ){
            $sql_result	= mysql_query('
                select discount_value
                from discount, products
                where
                    products.ID='.$iID.' and
                    products.SC_ID = discount.ID_category and
                    discount.ID_user = '.$this->iUserID
            );
            $sql_object	= mysql_fetch_object($sql_result);
            if(
                $sql_object->discount_value != '' and
                is_numeric($sql_object->discount_value) and
                $sql_object->discount_value>=0
            )
            $fPrice = $fPrice*((100-$sql_object->discount_value)/100);
        }
	}
        
    public function getRawNumericPrice($fPrice, $iCount){
        return $fPrice*$iCount;
    }

    public function getRawNumericPriceWithTax($fPrice, $iCount, $fTax){
        return $fPrice*(($fTax/100)+1)*$iCount;
    }
	
    public function simplePrice($iID,$iCount){
        $fPrice=0;
        $fTax=0;
        self::getDBPriceVat($iID,$fPrice,$fTax);
        if(_VAT_=='YES'){
            $result = $this->RoundIt_Euro($this->getRawNumericPriceWithTax($fPrice, $iCount, $fTax)).' '._CURRENCY1_.' '._i18n_product_WITH_VAT_;
            return $result;
        }
        else{
            $result = $this->RoundIt_Euro($this->getRawNumericPrice($fPrice, $iCount)).' '._CURRENCY1_;
            return $result;
        }
    }
	
    public function detailPrice($iID,$iCount){
        $fPrice=0;
        $fTax=0;
        self::getDBPriceVat($iID,$fPrice,$fTax);
        if(_VAT_=='YES')
            return
                $this->RoundIt_Euro(self::getRawNumericPriceWithTax($fPrice, $iCount, $fTax)).' '._CURRENCY1_.' '._i18n_product_WITH_VAT_.
                '<br>'.
                $this->RoundIt_Euro(self::getRawNumericPrice($fPrice, $iCount)).' '._CURRENCY1_.' '._i18n_product_WITHOUT_VAT_;
        else
                return 	$this->RoundIt_Euro(self::getRawNumericPrice($fPrice, $iCount)).' '._CURRENCY1_;
    }
	
    public function simpleResult($ProductsList, $CountsList){
        $iIndex 	= 0;
        $iCount 	= count($ProductsList);
        $fullPrice 	= 0;

        //return implode(' ',$ProductsList);

        if(_VAT_=='YES'){
            for($iIndex=0;$iIndex<$iCount;$iIndex++){
                self::getDBPriceVat($ProductsList[$iIndex],$fPrice,$fTax);
                $fullPrice += self::getRawNumericPriceWithTax($fPrice, $CountsList[$iIndex], $fTax);
            }
            $result = self::RoundIt_Euro($fullPrice).' '._CURRENCY1_.' '._i18n_product_WITH_VAT_;

            return $result;
        }
        else{
            for($iIndex=0;$iIndex<$iCount;$iIndex++){
                    self::getDBPriceVat($ProductsList[$iIndex],$fPrice,$fTax);
                    $fullPrice += self::getRawNumericPrice($fPrice, $CountsList[$iIndex]);
            }
            $result = self::RoundIt_Euro($fullPrice).' '._CURRENCY1_;

            return $result;

        }
    }
	
	public function detailResult($ProductsList, $CountsList){			
		$iIndex 	= 0;
		$iCount 	= count($ProductsList);
		$fullPrice 	= 0;
		
		//return implode(' ',$ProductsList);
		
		if(_VAT_=='YES'){
			for($iIndex=0;$iIndex<$iCount;$iIndex++){
				self::getDBPriceVat($ProductsList[$iIndex],$fPrice,$fTax);
				$fullPrice += self::getRawNumericPriceWithTax($fPrice, $CountsList[$iIndex], $fTax);
				$fullPrice_NoVAT += self::getRawNumericPrice($fPrice, $CountsList[$iIndex]);
			}
			$result =  	self::RoundIt_Euro($fullPrice).' '._CURRENCY1_.' '._i18n_product_WITH_VAT_.
                        '<br>'.
                        self::RoundIt_Euro($fullPrice_NoVAT).' '._CURRENCY1_.' '._i18n_product_WITHOUT_VAT_;
            return $result;

		}
		else{			
			for($iIndex=0;$iIndex<$iCount;$iIndex++){
				self::getDBPriceVat($ProductsList[$iIndex],$fPrice,$fTax);
				$fullPrice += self::getRawNumericPrice($fPrice, $CountsList[$iIndex]);
			}
            $result = self::RoundIt_Euro($fullPrice).'  '._CURRENCY1_;

            return $result;
		}
	}

	public function simpleTableAddOn_header(){	
		if(_VAT_=='YES')
			return '
				<td>
					<b>'._i18n_product_PRICE_FOR_UNIT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_VAT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_PRICE_WITH_VAT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_COUNT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_SUM_.'</b>
				</td>
			';
		else
			return '
				<td>
					<b>'._i18n_product_PRICE_FOR_UNIT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_COUNT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_SUM_.'</b>
				</td>
			';
	}
	
	public function simpleTableAddOn_values($iID,$iCount){
		$fPrice=0;
		$fTax=0;
		self::getDBPriceVat($iID,$fPrice,$fTax);
		if(_VAT_=='YES')
			return '
				<td>
					'.self::RoundIt_Euro(self::getRawNumericPriceWithTax($fPrice, 1, 0)).'
				</td>
				<td>
					'.$fTax.'
				</td>
				<td>
					'.self::RoundIt_Euro(self::getRawNumericPriceWithTax($fPrice, 1, $fTax)).'
				</td>
				<td>
					'.$iCount.'
				</td>
				<td>
					'.self::RoundIt_Euro(self::getRawNumericPriceWithTax($fPrice, $iCount, $fTax)).'
				</td>
			';
		else
			return '
				<td>
					'.self::RoundIt_Euro(self::getRawNumericPrice($fPrice, 1)).'
				</td>
				<td>
					'.$iCount.'
				</td>
				<td>
					'.self::RoundIt_Euro(self::getRawNumericPrice($fPrice, $iCount)).'
				</td>
			';
	}
	
	public function detailTableAddOn_header(){
		if(_VAT_=='YES')
			return '
				<td>
					<b>'._i18n_product_PRICE_FOR_UNIT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_VAT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_PRICE_WITH_VAT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_COUNT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_SUM_WITHOUT_VAT.'</b>
				</td>
				<td>
					<b>'._i18n_product_SUM_WITH_VAT.'</b>
				</td>
			';
		else
			return '
				<td>
					<b>'._i18n_product_PRICE_FOR_UNIT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_COUNT_.'</b>
				</td>
				<td>
					<b>'._i18n_product_SUM_.'</b>
				</td>
			';
	}
	
	public function detailTableAddOn_values($iID,$iCount){}
	
	public function RoundIt_SKK($FloatNumber){			//1,234
	    $RoughRound     = round($FloatNumber,2);			//1,23
	    $FloatElement   = fmod($RoughRound,1);			//0,23
	    $IntElement     = $RoughRound-$FloatElement;		//1
	    if (($FloatElement>=0) && ($FloatElement<0.25))             //0 az 0.24 zaokruhluje dolu
	        return number_format($IntElement, 2, ',', ' ');
	
	    if (($FloatElement>=0.25) && ($FloatElement<0.75))          //0.25 az 0,74 zaokruhluje na 0.5
	        return number_format(($IntElement+0.5), 2, ',', ' ');
	
	    if (($FloatElement>=0.75) && ($FloatElement<=0.99))         //0.75 az 0.99 zaokr. cele hore
	        return number_format(($IntElement+1), 2, ',', ' ');
	}
	
	public function RoundIt_Euro($FloatNumber){
		return number_format(round($FloatNumber,2), 2, ',', ' ');
	}
}
?>